package com.ruoyi.ftrl.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ftrl.domain.RoomInfo;
import com.ruoyi.ftrl.mapper.RoomInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ftrl.mapper.EquipmentHeatMapper;
import com.ruoyi.ftrl.domain.EquipmentHeat;
import com.ruoyi.ftrl.service.IEquipmentHeatService;

/**
 * 热量采集器设备管理Service业务层处理
 * 
 * @author dihang
 * @date 2024-08-09
 */
@Service
public class EquipmentHeatServiceImpl implements IEquipmentHeatService 
{
    @Autowired
    private EquipmentHeatMapper equipmentHeatMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 查询热量采集器设备管理
     * 
     * @param id 热量采集器设备管理主键
     * @return 热量采集器设备管理
     */
    @Override
    public EquipmentHeat selectEquipmentHeatById(Long id)
    {
        return equipmentHeatMapper.selectEquipmentHeatById(id);
    }

    /**
     * 查询热量采集器设备管理列表
     * 
     * @param equipmentHeat 热量采集器设备管理
     * @return 热量采集器设备管理
     */
    @Override
    public List<EquipmentHeat> selectEquipmentHeatList(EquipmentHeat equipmentHeat)
    {
        return equipmentHeatMapper.selectEquipmentHeatList(equipmentHeat);
    }

    /**
     * 新增热量采集器设备管理
     * 
     * @param equipmentHeat 热量采集器设备管理
     * @return 结果
     */
    @Override
    public int insertEquipmentHeat(EquipmentHeat equipmentHeat)
    {
        equipmentHeat.setCreateTime(DateUtils.getNowDate());
        equipmentHeat.setCreateBy(SecurityUtils.getUsername());
        //新增设备时将设备绑定的房间编号带入
        RoomInfo roomInfo=new RoomInfo();
        roomInfo.setCommunityName(equipmentHeat.getCommunityName());
        roomInfo.setBuildingName(equipmentHeat.getBuildingName());
        roomInfo.setUnitName(equipmentHeat.getUnitName());
        roomInfo.setHouseNumber(equipmentHeat.getRoomAme());
        List<RoomInfo> roomInfos = roomInfoMapper.selectRoomInfoList(roomInfo);
        //如果设备绑定房间不存在，抛出异常 //todo
        RoomInfo roomInfo1 = roomInfos.get(0);
        if (roomInfo1==null){
            throw new ServiceException("您所绑定该设备对应的房间不存在，请先添加后再进行绑定！");
        }
        equipmentHeat.setRoomNo(roomInfo1.getRoomNo());
        //户主姓名
        equipmentHeat.setUserName(roomInfo1.getPersonName());
        return equipmentHeatMapper.insertEquipmentHeat(equipmentHeat);
    }

    /**
     * 修改热量采集器设备管理
     * 
     * @param equipmentHeat 热量采集器设备管理
     * @return 结果
     */
    @Override
    public int updateEquipmentHeat(EquipmentHeat equipmentHeat)
    {
        equipmentHeat.setUpdateTime(DateUtils.getNowDate());
        return equipmentHeatMapper.updateEquipmentHeat(equipmentHeat);
    }

    /**
     * 批量删除热量采集器设备管理
     * 
     * @param ids 需要删除的热量采集器设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentHeatByIds(Long[] ids)
    {
        return equipmentHeatMapper.deleteEquipmentHeatByIds(ids);
    }

    /**
     * 删除热量采集器设备管理信息
     * 
     * @param id 热量采集器设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentHeatById(Long id)
    {
        return equipmentHeatMapper.deleteEquipmentHeatById(id);
    }
}
